package org.opu.doc2db.document.txtdoc.io;

import java.util.List;

public class TableColumnWriter {

    private Content content;
    private StringBuilder sb;
    private SettingsCalculator calculator;

    public TableColumnWriter() {
        this(new SettingsCalculator());
    }

    public TableColumnWriter(SettingsCalculator calculator) {
        this.calculator = calculator;
        this.content = new Content();
        this.sb = new StringBuilder();
    }

    public void addNewRow() {
        doFlush();
        content.add(new Row());
    }

    public void addNewCell(List<String> strings, int split) {
        content.add(new Cell(strings, split));
    }

    private void doFlush() {
        // now reason at the beginning
        if (content.isEmpty()) {
            return;
        }

        // first normal row.
        for (Cell c : content.last()) {
            if (c.nullable()) {
                return;
            }
        }

        Row last = content.last();
        content.rows.remove(last);

        doToString();

        content = new Content();
        content.rows.add(last);
    }

    private void doToString() {
        char spc = TextDocumentWriter.SPACE;
        char spr = TextDocumentWriter.CEL_SEPARATOR;

        int cmax = content.cmax();
        int rmax = content.rmax();

        int[] sizes = colSizes(cmax);
        String  buff[][] = new String[rmax][cmax];

        for (int i = 0; i < rmax; i++) {
            for (int j = 0; j < cmax; j++) {
                String get = content.get(i, j);
                if (get == null){
                    if (j + 1 >= cmax){ // last cell is empty.
                        get = "" + spr;
                    } else {
                        int sz = sizes[j];

                        if (i > 0){
                            sz = buff[i - 1][j].length();
                        } 

                        if (j > 0){
                            get = spr + calculator.add(null, spc, sz - 1);
                        } else {
                            get = calculator.add(null, spc, sz);
                        }
                    }
                }
                buff[i][j] = get;
            }
        }

        sb.append(toString(buff));
    }

    @Override
    public String toString() {
        doToString();
        return sb.toString();
    }

    /**
     * Return the preferred size for each column
     * @param cmax number of culumn that can be (max value)
     * @return the preferred size for each column
     */
    int[] colSizes(int cmax) {
        int[] ret = new int[cmax];

        for (Row r : content) {
            
            int j = 0;
            for (Cell c : r) {
                if (!c.nullable()) {
                    for (String s : c) {
                        int len = s.length();
                        if (len > ret[j]) {
                            ret[j] = len;
                        }
                    }
                }
                
                j ++;
            }
        }

        return ret;
    }

    String toString(String [][] arr){
        StringBuilder buff = new StringBuilder();

        for (String[] arrS : arr) {
            for (String s : arrS) {
                buff.append(s);
            }

            buff.append(calculator.getNewLine(null));
        }

        return buff.toString();
    }
}
